<template>
  <el-dialog :title="titlePark" append-to-body :close-on-click-modal="false" @close="closeDialog()" v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="区域名称" prop="regionName">
            <el-input v-model="dataForm.regionName" placeholder="区域名称" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车位号" prop="lotNo">
            <el-input v-model="dataForm.lotNo" placeholder="车位号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="车位状态" prop="status">
            <el-select v-model="dataForm.status" clearable class="m-2" placeholder="车位状态">
              <el-option v-for="item in dicts.park_space" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车牌号" prop="plateNumber">
            <el-input v-model="dataForm.plateNumber" placeholder="车牌号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="租赁开始日期" prop="startDate">
            <el-date-picker v-model="dataForm.startDate" type="date" value-format="YYYY-MM-DD" placeholder="租赁开始日期" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="租赁结束日期" prop="endDate">
            <el-date-picker v-model="dataForm.endDate" type="date" value-format="YYYY-MM-DD" placeholder="租赁结束日期" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button @click="dataFormSubmit()" type="primary">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from '@/api/stop/regionlot'

export default {
  dicts: ['park_space'],
  props: ["regionNames", "regionId"],
  data () {
    return {
      visible: false,
      canSubmit: false,
      dataForm: {
        regionName: null,
        regionId: null,
        isSync: 0
      },
      dataRule: {
        lotNo: [
          { required: true, message: '车位号不能为空', trigger: 'blur' }
        ],

        isSync: [
          { required: true, message: '是否同步不能为空', trigger: 'blur' }
        ],

        startDate: [
          { required: true, message: '租赁开始日期不能为空', trigger: 'blur' }
        ],

        endDate: [
          { required: true, message: '租赁结束日期不能为空', trigger: 'blur' }
        ],

        insideId: [
          { required: true, message: '内部客户id不能为空', trigger: 'blur' }
        ],

        regionId: [
          { required: true, message: '区域id不能为空', trigger: 'blur' }
        ],

        status: [
          { required: true, message: '0 未占用 1 已锁定待付款 2 租赁中 3 路边车位已占用不能为空', trigger: 'blur' }
        ],

        timeOutTime: [
          { required: true, message: '锁定超时时间-过了时间要改为0不能为空', trigger: 'blur' }
        ],

        groupCode: [
          { required: true, message: 'groupCode不能为空', trigger: 'blur' }
        ],

        plateNumber: [
          { required: true, message: '车牌号不能为空', trigger: 'blur' }
        ],

        hardwareSn: [
          { required: true, message: '地磁序号列不能为空', trigger: 'blur' }
        ],

        enterId: [
          { required: true, message: '入场记录id不能为空', trigger: 'blur' }
        ],

      },
      titlePark: null
    }
  },
  methods: {
    init (id) {
      this.dataForm.regionName = this.regionNames.regionNames
      this.dataForm.regionId = this.regionId.regionId
      if (id == undefined) {
        this.titlePark = '新增'
      } else {
        this.titlePark = '修改'
      }
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (id) {
          getObj(id).then(response => {
            this.dataForm = response.data.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    },
    //重置表单
    closeDialog () {
      this.$refs["dataForm"].resetFields()
    }
  }
}
</script>
